# -*- coding: utf-8 -*-

'''
2017-6-3 pylint format
'''
import shutil
import sys
import os

# ----------------------------------------------------------------------------
# Use For path control!
# ----------------------------------------------------------------------------

def __addpathprefix(path_str):
    if sys.platform != "win32":
        return path_str
    if path_str.startswith("\\\\?\\"):
        return path_str
    ret = "\\\\?\\" + os.path.abspath(path_str)
    ret = ret.replace("/", "\\")
    return ret


def get_dirname(dirpath):
    '''
        获取文件夹名字
        ex:getDirName("c:\\test")
    '''
    if not os.path.isdir(dir):
        return
    tmp = dirpath.split("\\")
    name = tmp[len(tmp) - 1]
    return name


def copyfiles2_dir(fromdir, todir):
    '''
        复制fromdir文件夹下的文件和文件夹到dst  fromdir必须是目录
        ex:copyFilestoDir("c:\\test" , "c:\\test2")
    '''
    if not os.path.isdir(todir):
        os.makedirs(todir)
    for item in os.listdir(fromdir):
        path = os.path.join(fromdir, item)
        if os.path.isfile(path):
            path = __addpathprefix(path)
            copy_dst = __addpathprefix(todir)
            shutil.copy(path, copy_dst)
        if os.path.isdir(path):
            new_dst = os.path.join(todir, item)
            if not os.path.isdir(new_dst):
                os.makedirs(__addpathprefix(new_dst))
            copyfiles2_dir(path, new_dst)


def delfiles_bytype(tarpath, typelist):
    '''
        删除dir目录下所有type类型的文件
        ex:deleTypesFiles("c:\\test" , [".so" , ".jpg"])
    '''
    for typename in typelist:
        for filename in os.listdir(tarpath):
            item = os.path.join(tarpath, filename)
            if os.path.isfile(item) and os.path.basename(filename).find(typename) != -1:
                os.remove(item)
            if os.path.isdir(item):
                delfiles_bytype(item, typelist)
